﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using FANUC.DTO;

namespace FANUC.Controllers
{
    public class SevenDaysAllRobotsOutputsController : ApiController
    {
        private FANUCDB db = new FANUCDB();

        [ResponseType(typeof(SevenDaysAllRobotsOutputsBarDtoOutput))]
        public IHttpActionResult Get()
        {
            #region Test
            if (Helper.IsTest)
            {
                DateTime now = System.DateTime.Now.Date;
                SevenDaysAllRobotsOutputsBarDtoOutput testData = new SevenDaysAllRobotsOutputsBarDtoOutput();
                testData.XAxisTitle = new List<string>() { now.AddDays(-6).ToString("yy-MM-dd"), now.AddDays(-5).ToString("yy-MM-dd"), now.AddDays(-4).ToString("yy-MM-dd"), now.AddDays(-3).ToString("yy-MM-dd"), now.AddDays(-2).ToString("yy-MM-dd"), now.AddDays(-1).ToString("yy-MM-dd"), now.ToString("yy-MM-dd") };
                testData.Data = new List<decimal> { 1000M, 1200M, 1150M, 1100M, 1120M, 1300M, 1250M };
                return Ok(testData);
            }
            #endregion
            DateTime today = System.DateTime.Now.Date;
            DateTime sevenDays = today.AddDays(-6);
            var perDayDataArc = (from w in db.Part_Unit.Where(a => a.Start_Time >= sevenDays)
                                 group w by new { w.Start_Time.Year, w.Start_Time.Month, w.Start_Time.Day }
                                  into grp
                                 select new
                                 {
                                     grp.Key.Year,
                                     grp.Key.Month,
                                     grp.Key.Day,
                                     TheDayTotalOutputProductQuantity = grp.Count(),
                                 });
            var perDayDataSpot = (from w in db.Spot_Part_Unit.Where(a => a.Start_Time >= sevenDays)
                                  group w by new { w.Start_Time.Year, w.Start_Time.Month, w.Start_Time.Day }
                                 into grp
                                  select new
                                  {
                                      grp.Key.Year,
                                      grp.Key.Month,
                                      grp.Key.Day,
                                      TheDayTotalOutputProductQuantity = grp.Count(),
                                  });
            var perDayDataSeal = (from w in db.Seal_Part_Unit.Where(a => a.Start_Time >= sevenDays)
                                  group w by new { w.Start_Time.Year, w.Start_Time.Month, w.Start_Time.Day }
                                 into grp
                                  select new
                                  {
                                      grp.Key.Year,
                                      grp.Key.Month,
                                      grp.Key.Day,
                                      TheDayTotalOutputProductQuantity = grp.Count(),
                                  });
            var perDayDataPolish = (from w in db.Polish_Part_Unit.Where(a => a.Start_Time >= sevenDays)
                                    group w by new { w.Start_Time.Year, w.Start_Time.Month, w.Start_Time.Day }
                                 into grp
                                    select new
                                    {
                                        grp.Key.Year,
                                        grp.Key.Month,
                                        grp.Key.Day,
                                        TheDayTotalOutputProductQuantity = grp.Count(),
                                    });
            var perDayDataPress = (from w in db.Press_Part_Unit.Where(a => a.Start_Time >= sevenDays)
                                   group w by new { w.Start_Time.Year, w.Start_Time.Month, w.Start_Time.Day }
                                 into grp
                                   select new
                                   {
                                       grp.Key.Year,
                                       grp.Key.Month,
                                       grp.Key.Day,
                                       TheDayTotalOutputProductQuantity = grp.Count(),
                                   });
            var perDayData = (perDayDataArc.Concat(perDayDataSpot).Concat(perDayDataSeal).Concat(perDayDataPolish).Concat(perDayDataPress)).OrderBy(o => o.Year).ThenBy(o => o.Month).ThenBy(o => o.Day).ToList();
            if (perDayData.Count() <= 0)
            {
                SevenDaysAllRobotsOutputsBarDtoOutput noData = new SevenDaysAllRobotsOutputsBarDtoOutput();
                noData.XAxisTitle = new List<string>() { today.AddDays(-6).ToString("yy-MM-dd"), today.AddDays(-5).ToString("yy-MM-dd"), today.AddDays(-4).ToString("yy-MM-dd"), today.AddDays(-3).ToString("yy-MM-dd"), today.AddDays(-2).ToString("yy-MM-dd"), today.AddDays(-1).ToString("yy-MM-dd"), today.ToString("yy-MM-dd") };
                noData.Data = new List<decimal> { 0, 0, 0, 0, 0, 0, 0 };
                return Ok(noData);
            }

            SevenDaysAllRobotsOutputsBarDtoOutput data = new SevenDaysAllRobotsOutputsBarDtoOutput();
            data.XAxisTitle = new List<string>() { today.AddDays(-6).ToString("yy-MM-dd"), today.AddDays(-5).ToString("yy-MM-dd"), today.AddDays(-4).ToString("yy-MM-dd"), today.AddDays(-3).ToString("yy-MM-dd"), today.AddDays(-2).ToString("yy-MM-dd"), today.AddDays(-1).ToString("yy-MM-dd"), today.ToString("yy-MM-dd") };
            data.Data = new List<decimal> { 0, 0, 0, 0, 0, 0, 0 };
            foreach (var item in perDayData)
            {
                string itemDate = new DateTime(item.Year, item.Month, item.Day).ToString("yy-MM-dd");
                for (int i = 0; i < data.XAxisTitle.Count; i++)
                {
                    if (data.XAxisTitle[i] == itemDate)
                    {
                        data.Data[i] = item.TheDayTotalOutputProductQuantity;
                    }
                }
            }
            return Ok(data);
        }

        [ApiExplorerSettings(IgnoreApi = true)]
        public string Options()
        { return null; }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
